package cn.acmoj.oj.controller;

import cn.acmoj.oj.bean.Contest;
import cn.acmoj.oj.bean.SolutionPro;
import cn.acmoj.oj.service.ContestService;
import cn.acmoj.oj.service.SolutionService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * cn.acmoj.oj.controller
 * Springer
 * 2017-11-22
 */
@Controller
@RequestMapping(value = "contest")
public class ContestStatusController {

    @Resource
    private SolutionService solutionService;
    @Resource
    private ContestService contestService;

    private final String[] status = {
            "等待评测",
            "重判中",
            "编译中",
            "运行并评判",
            "正确",
            "格式错误",
            "答案错误",
            "时间超限",
            "内存超限",
            "输出超限",
            "运行错误",
            "编译错误",
            "编译成功",
            "运行完成"
    };

    private final String[] lang = {
            "C",
            "C++",
            "Pascal",
            "Java",
            "Ruby",
            "Bash",
            "Python"
    };

    @RequestMapping(value = "status", method = RequestMethod.GET)
    public String status(@RequestParam(required = false, defaultValue = "1") Integer page,
                         @RequestParam(required = false, defaultValue = "") String uid,
                         @RequestParam(required = false, defaultValue = "-1") Integer lang,
                         @RequestParam(required = false, defaultValue = "-1") Integer pid,
                         @RequestParam(required = false, defaultValue = "-1") Integer result,
                         @RequestParam(required = false, defaultValue = "-1") Integer cid,
                         HttpServletRequest request,
                         HttpSession session) {
        if (cid != null && cid == -1) {
            cid = (Integer) session.getAttribute("cid");
        }
        if (cid == null) {
            return "contest/error";
        }
        Contest contest = contestService.getOnce(cid);
        if (pid == null) {
            pid = -1;
        }
        if (lang == null) {
            lang = -1;
        }
        if (page == null) {
            page = 1;
        }
        if (result == null) {
            result = -1;
        }
        int num = 20;
        int count = solutionService.getCount(cid, pid, uid, lang, result);
        int pageCount = count / 20 + 1;
        if (count % num == 0) {
            pageCount--;
        }
        List<SolutionPro> list = solutionService.getAll(cid, page, num, pid, uid, lang, result);
        request.setAttribute("list", list);
        request.setAttribute("contest", contest);
        request.setAttribute("status", this.status);
        request.setAttribute("lang", this.lang);
        request.setAttribute("count", pageCount);
        return "contest/status";
    }

}
